import { formatGMTDate} from '../../../../assets/js/util.js'
export default {
    data() {
        return {
            // 默认参数 和 URL
            SRC: '',
            DP: {
                p: '',
                f: ''
            },
            /*当前页显示数量*/
            pageSize: 10,
            /*每页显示的数量*/
            pageSizes: [10, 20, 50, 100],
            /*dataTotal表数量*/
            dataTotal: 0,
            /*分页器*/
            currentPage: 1,
            ruleForm: {
                curPage: 1,
                pageSize: 10,
                addStartDate: '',
                addEndDate: '',
            },
            tableHeader: [
                { "prop": "description", "label": "监控说明" },
                { "prop": "serverName", "label": "服务器名称" },
                { "prop": "subDescription", "label": "监控产出说明" },
                { "prop": "userOrGroupName", "label": "产出值" },
                { "prop": "result", "label": "告警值" },
                { "prop": "creatTime", "label": "告警时间" }
            ],
            tableData: [],
        }
    },
    methods: {
        // 分页器
        // 控制每页的数量
        handleSizeChange(val) {
            this.pageSize = val;
            this.getTableData(`curPage=${this.currentPage}&pageSize=${this.pageSize}&dateStart=${this.ruleForm.addStartDate}&dateEnd=${this.ruleForm.addEndDate}`);
        },
        // 页数
        handleCurrentChange(val) {
            this.currentPage = val;
            this.getTableData(`curPage=${this.currentPage}&pageSize=${this.pageSize}&dateStart=${this.ruleForm.addStartDate}&dateEnd=${this.ruleForm.addEndDate}`);
        },
        findForm() {

        },
        addForm() {

        },
        resetForm() {

        },
        successFun(data) {
            this.$message({
                message: data,
                type: 'success'
            });
        },
        errorFun(data) {
            this.$message.error(data);
        },
        handleClick() {},
        dateChange1(val) {
            this.ruleForm.addStartDate = val;
        },
        dateChange2(val) {
            this.ruleForm.addEndDate = val;
        },
        //查找功能实现
        findForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    var d = this.ruleForm;
                    var obj = {
                        curPage: this.currentPage,
                        pageSize: this.pageSize,
                        dateStart: d.addStartDate,
                        dateEnd: d.addEndDate,
                    };
                    var str = '';
                    for (var i in obj) {
                        if (obj[i] !== "") {
                            str += `${i}=${obj[i]}&`
                        }
                    }
                    str = str.substring(0, str.length - 1);
                    this.getTableData(str);
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        // 获取表格的数据
        getTableData(url) {
            var d = this.ruleForm;
            url = encodeURI(url);
            this.$http.jsonp(`${this.SRC}data/get.do`, {
                params: {
                    p: this.DP.p,
                    f: this.DP.f,
                    a: "list",
                    ps: url
                }
            }).then((response) => {
                var data = JSON.parse(response.body)
                var response = JSON.parse(data.replace(/\"\{\"/g, '\{\"').replace(/\"\}\"/g, '\"\}'));
                this.dataTotal = response.msg.totalSize;
                var el = response.msg.data;
                for (var i = 0; i < el.length; i++) {
                    function getLocalTime(nS) {
                        return new Date(parseInt(nS)).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
                    }
                    getLocalTime(el[i].creatTime);
                    el[i].creatTime = getLocalTime(el[i].creatTime);
                };
                this.tableData = response.msg.data;

            });
        },
    },
    created() {
        // 初始化URL
        this.SRC = this.$store.getters.src;
        this.DP.p = localStorage.getItem('ZULONGBIPROJECTID');
        this.DP.f = localStorage.getItem('ZULONGBIPAGEID');
        this.ruleForm.addStartDate = formatGMTDate(new Date().getTime() - 86400000);
        this.ruleForm.addEndDate = formatGMTDate(new Date().getTime());
        this.getTableData(`curPage=${this.currentPage}&pageSize=${this.pageSize}`);
    }
}